//
//  AppDelegate.m
//  MMusic
//
//  Created by chenchao on 15/12/9.
//  Copyright © 2015年 iOS. All rights reserved.
//

#import "AppDelegate.h"
#import "MMAdController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ---------------- 启动页延时3秒-------------------------//
    [NSThread sleepForTimeInterval:3];
    //1. 创建Window
    self.window.frame = [UIScreen mainScreen].bounds;
    [self.window makeKeyAndVisible];
    
    //2. 加载启动页之后，进入广告页，参照支付宝和一些电商应用，方便以后动态加载
    MMAdController *adVC = [[MMAdController alloc] init];
    [self.window setRootViewController:adVC];
    
    //3. 设置缓存策略
    [self setCachEStrategy];
    
    //4. 从沙盒中读取用户信息
    
    //5. 监控网络状态转变
    //5.1  初始化
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    //5.2  调用监听
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:          // 未知网络
                break;
            case AFNetworkReachabilityStatusNotReachable:     // 没有网络(断网)
            {
                [SVProgressHUD showWithStatus:@"网络异常，请检查网络设置"];
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                break;
        }
    }];
    //5.3   开启监控网络连接状体的变化
    [mgr startMonitoring];
    
 
    //6.  消息推送注册  iOS8之后 API有所改变  现在要先设置好setting
    //6.1 ios8  消息推送的注册
    // -------------------- 启动应用时会征询用户是否同意“喵喵音乐”推送通知----------------------------
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeAlert|
                                                                                            UIRemoteNotificationTypeBadge|
                                                                                            UIRemoteNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:setting];
    } else {  //6.2 iOS8 之前的方式
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }

    
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


#pragma mark -    相关项目配置
-(void)setCachEStrategy{
    int cacheSizeMemory = 4*1024*1024; // 4MB
    int cacheSizeDisk = 32*1024*1024; // 32MB
    NSURLCache *shareCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:shareCache];
}


@end
